package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortCartData;
import com.yanqu.road.entity.beautyescort.enums.eEscortCartState;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_DART_TIME_REDUCE, desc = "镖车加速")
public class CrossBeautyEscortReduceDartTimeCmd extends CrossCommand {

    int cartId;
    int time;   //每个道具加速时间
    int goodsNum;
    long playerId;
    int realNum;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortReduceDartTimeReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortReduceDartTimeReqMsg.parseFrom(packet.getBytes());
        cartId = reqMsg.getCartId();
        time = reqMsg.getTime();
        goodsNum = reqMsg.getGoodsNum();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossEscortPlayerWrap playerWrap= CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(playerWrap != null){
            for (Map.Entry<Integer, EscortCartData> cartDataEntry : playerWrap.getCartDataMap().entrySet()) {
                EscortCartData cartData = cartDataEntry.getValue();
                if(cartData.getCartId() == cartId){
                    synchronized (cartData){
                        if(cartData.getState() != eEscortCartState.Escorting.getIntValue()){
                            ret = GameErrorCode.E_BEAUTY_ESCORT_IS_FINISH;
                            return;
                        }
                        int allNeed = 60 * (cartData.getTakeTime() - cartData.getReduceTime());
                        int passSecond = DateHelper.getCurrentSecond() - cartData.getStartTime();
                        int needSecond = allNeed - passSecond;
                        if(needSecond <= 0){
                            ret = GameErrorCode.E_BEAUTY_ESCORT_IS_FINISH;
                            return;
                        }
                        int needNum;
                        if(needSecond % (time * 60) == 0){
                            needNum = needSecond / (time * 60);
                        }else {
                            needNum = needSecond / (time * 60) + 1;
                        }
                        if(needNum > goodsNum){
                            needNum = goodsNum;
                        }
                        cartData.setReduceTime(cartData.getReduceTime() + time * needNum);
                        //log.info("镖车加速 : {},{},{}", packet.getPlayerId(), cartId, time * needNum);
                        realNum = needNum;
                        playerWrap.calCartStateWithEvent(cartData);
                    }
                    ret = 0;
                    break;
                }else {
                    ret = GameErrorCode.E_BEAUTY_ESCORT_CART_NO_EXIST;
                }
            }
        }
    }

    @Override
    public void sendData() {
        //返回日志
        BeautyEscortProto.CrossBeautyEscortReduceDartTimeRespMsg.Builder respMsg = BeautyEscortProto.CrossBeautyEscortReduceDartTimeRespMsg.newBuilder();
        respMsg.setCartId(cartId);
        respMsg.setGoodsNum(goodsNum);
        respMsg.setRet(ret);
        respMsg.setRealNum(realNum);
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_DART_TIME_REDUCE_FROM_CROSS, respMsg));
    }
}
